<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Netty ChannelFuture 与异步编程深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        /* Hero 区域样式 */
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: pulse 15s ease-in-out infinite;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(1) rotate(0deg); }
            50% { transform: scale(1.1) rotate(180deg); }
        }
        
        /* 代码块样式 */
        pre {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            padding: 1.5rem;
            overflow-x: auto;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            position: relative;
            margin: 2rem 0;
        }
        
        pre::before {
            content: 'JAVA';
            position: absolute;
            top: 0.5rem;
            right: 1rem;
            font-size: 0.75rem;
            color: #666;
            font-weight: 600;
            letter-spacing: 0.1em;
        }
        
        code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            font-size: 0.9rem;
            line-height: 1.6;
        }
        
        /* 内联代码样式 */
        .inline-code {
            background: linear-gradient(135deg, #e0e7ff 0%, #f0e6ff 100%);
            color: #5b21b6;
            padding: 0.2rem 0.5rem;
            border-radius: 6px;
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            font-size: 0.9em;
            font-weight: 500;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        
        /* 卡片悬停效果 */
        .card-hover {
            transition: all 0.3s ease;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.15);
        }
        
        /* 标题装饰 */
        .section-title {
            position: relative;
            padding-left: 1.5rem;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 80%;
            background: linear-gradient(180deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        /* 首字下沉 */
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        /* 列表样式 */
        .custom-list {
            counter-reset: item;
            list-style: none;
        }
        
        .custom-list li {
            counter-increment: item;
            position: relative;
            padding-left: 3rem;
            margin-bottom: 1.5rem;
        }
        
        .custom-list li::before {
            content: counter(item);
            position: absolute;
            left: 0;
            top: 0;
            width: 2rem;
            height: 2rem;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 700;
            font-size: 0.9rem;
        }
        
        /* Mermaid 图表容器 */
        .mermaid-container {
            background: white;
            border-radius: 12px;
            padding: 2rem;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            margin: 2rem 0;
        }
        
        /* 滚动动画 */
        .fade-in {
            opacity: 0;
            transform: translateY(20px);
            animation: fadeIn 0.8s ease forwards;
        }
        
        @keyframes fadeIn {
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .fade-in:nth-child(1) { animation-delay: 0.1s; }
        .fade-in:nth-child(2) { animation-delay: 0.2s; }
        .fade-in:nth-child(3) { animation-delay: 0.3s; }
        .fade-in:nth-child(4) { animation-delay: 0.4s; }
    </style>
</head>
<body>
    <!-- Hero 区域 -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto relative z-10">
            <div class="text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 tracking-tight">
                    <i class="fas fa-network-wired mr-4"></i>
                    Netty ChannelFuture
                </h1>
                <p class="text-xl md:text-2xl mb-8 opacity-90">深入理解异步编程处理模型</p>
                <div class="flex justify-center space-x-6 text-sm">
                    <span><i class="fas fa-clock mr-2"></i>阅读时间：15分钟</span>
                    <span><i class="fas fa-layer-group mr-2"></i>难度：中级</span>
                    <span><i class="fas fa-code mr-2"></i>代码示例：丰富</span>
                </div>
            </div>
        </div>
    </section>

    <!-- 主要内容区域 -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- 引言卡片 -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover fade-in">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>Netty 中，<span class="inline-code">ChannelFuture</span> 和异步编程处理模型是实现高性能 I/O 操作的核心，它们通过非阻塞方式处理异步事件，从而极大提高了网络通信的效率。<span class="inline-code">ChannelFuture</span> 提供了一个可以获取操作结果的方式，同时允许用户注册回调来响应操作完成的时刻。
            </p>
        </div>

        <!-- 概念关系图 -->
        <div class="mermaid-container fade-in">
            <h3 class="text-xl font-bold mb-4 text-gray-800">
                <i class="fas fa-sitemap mr-2 text-purple-600"></i>
                核心概念关系图
            </h3>
            <div class="mermaid">
                graph TD
                    A[Channel] -->|异步操作| B[ChannelFuture]
                    B -->|继承| C[Future接口]
                    B -->|注册| D[ChannelFutureListener]
                    D -->|回调| E[operationComplete]
                    B -->|状态| F[isSuccess]
                    B -->|状态| G[cause]
                    H[EventLoop] -->|执行| B
                    I[异步编程模型] -->|核心| B
                    I -->|优势| J[非阻塞]
                    I -->|优势| K[高性能]
            </div>
        </div>

        <!-- 第一部分：ChannelFuture 概述 -->
        <section class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover fade-in">
            <h2 class="text-3xl font-bold mb-6 section-title text-gray-800">
                1. ChannelFuture 概述
            </h2>
            <p class="text-lg leading-relaxed text-gray-700 mb-6 indent-8">
                <span class="inline-code">ChannelFuture</span> 是 Netty 中用于异步操作的关键接口，它表示一个尚未完成的 I/O 操作。通过 <span class="inline-code">ChannelFuture</span>，用户可以在操作完成后获取结果，或者注册回调来在完成时执行某些操作。
            </p>
            
            <pre><code>public interface ChannelFuture extends Future&lt;Void&gt; {
    Channel channel(); // 获取关联的 Channel
    boolean isSuccess(); // 判断操作是否成功
    Throwable cause(); // 获取失败原因
    ChannelFuture addListener(GenericFutureListener&lt;? extends Future&lt;? super Void&gt;&gt; listener); // 添加监听器
}</code></pre>
            
            <p class="text-lg leading-relaxed text-gray-700 indent-8">
                <span class="inline-code">ChannelFuture</span> 扩展了 <span class="inline-code">Future</span> 接口，提供了对 I/O 操作结果的访问。<span class="inline-code">addListener()</span> 方法用于注册监听器，以便在操作完成时执行回调。
            </p>
        </section>

        <!-- 第二部分：执行流程 -->
        <section class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover fade-in">
            <h2 class="text-3xl font-bold mb-6 section-title text-gray-800">
                2. ChannelFuture 的执行流程
            </h2>
            <p class="text-lg leading-relaxed text-gray-700 mb-6 indent-8">
                在 Netty 中，<span class="inline-code">ChannelFuture</span> 主要与异步 I/O 操作（如读写数据）配合使用。执行流程通常包括以下几个步骤：
            </p>
            
            <ol class="custom-list">
                <li>
                    <strong class="text-purple-700">发起异步操作</strong>：通过调用 <span class="inline-code">Channel</span> 上的异步方法（如 <span class="inline-code">write()</span>, <span class="inline-code">flush()</span>）发起操作。
                </li>
                <li>
                    <strong class="text-purple-700">返回 ChannelFuture</strong>：这些异步方法会返回一个 <span class="inline-code">ChannelFuture</span>，用于追踪操作的完成情况。
                </li>
                <li>
                    <strong class="text-purple-700">注册回调监听器</strong>：使用 <span class="inline-code">addListener()</span> 方法注册回调函数，回调将在操作完成时被触发。
                </li>
                <li>
                    <strong class="text-purple-700">操作完成通知</strong>：当操作完成时，<span class="inline-code">ChannelFuture</span> 会通知所有注册的监听器，执行回调。
                </li>
            </ol>
            
            <pre><code>public ChannelFuture write(Object msg) {
    // 异步写操作，返回 ChannelFuture
    return write(msg, newPromise());
}

private ChannelPromise newPromise() {
    // 返回新的 ChannelPromise 实例
    return new DefaultChannelPromise(this);
}</code></pre>
            
            <p class="text-lg leading-relaxed text-gray-700 indent-8 mt-6">
                在 <span class="inline-code">Channel</span> 类中，<span class="inline-code">write()</span> 方法发起了一个异步写操作，并返回一个 <span class="inline-code">ChannelFuture</span>。这个 <span class="inline-code">ChannelFuture</span> 可以用来注册回调监听器。
            </p>
        </section>

        <!-- 执行流程图 -->
        <div class="mermaid-container fade-in">
            <h3 class="text-xl font-bold mb-4 text-gray-800">
                <i class="fas fa-project-diagram mr-2 text-purple-600"></i>
                异步操作执行流程
            </h3>
            <div class="mermaid">
                sequenceDiagram
                    participant Client
                    participant Channel
                    participant EventLoop
                    participant Channel